package com.game.android;

import net.SocketEnum;

import org.apache.log4j.Logger;

import com.component.code.CommonCode;
import com.data.WDataConversion;
import com.dictionary.DictionaryTabel;
import com.game.model.DeskPlayer;
import com.game.serivce.DeskPlayerService;
/**
 * 机器人服务
 * @author wuxiaojian
 *
 */
public class AndroidService extends DeskPlayerService{

	private Logger logger = Logger.getLogger(AndroidService.class);
	public AndroidManage androidManage;
	public AndroidService() {}
	
	public AndroidService(int number, AndroidManage androidManage){
		getPlayerService().deskPlayer = new DeskPlayer();
		this.androidManage = androidManage;
		getPlayerService().deskPlayer.setPlayerId(10000000l + androidManage.gameRoomService.configRoomService.roomNumberForDb * 1000 + number + 1);
		getPlayerService().deskPlayer.setPlayerType(DictionaryTabel.PlayerType_Android);
		getPlayerService().deskPlayer.setPlayerSex(number % 2);
		changeAndroidInfo();
	}
	
	public void changeAndroidInfo() {
		int r = (int) (Math.random() * androidManage.getAndroidName().length);
		getPlayerService().deskPlayer.setNickName(androidManage.getAndroidName()[r]);
		getPlayerService().changePropNum(2, WDataConversion.randLong(androidManage.gameRoomService.configRoomService.androidHaveMoney[0],androidManage.gameRoomService.configRoomService.androidHaveMoney[1]));
		if (Math.random() < 0.3) {
			getPlayerService().deskPlayer.setHeadUrl(WDataConversion.randInt(0, 4) + "");
		} else {
			if (getPlayerService().deskPlayer.getPlayerSex() == CommonCode.Sex_WoMan) {
				getPlayerService().deskPlayer.setHeadUrl(WDataConversion.randInt(4, 8) + "");
			} else {
				getPlayerService().deskPlayer.setHeadUrl(WDataConversion.randInt(8, 12) + "");
			}
		}
		int vipLevel = 0;
		double b = Math.random();
		if (b < 0.005) {
			vipLevel = 5;
		} else if (b < 0.01) {
			vipLevel = 4;
		} else if (b < 0.04) {
			vipLevel = 3;
		} else if (b < 0.10) {
			vipLevel = 2;
		} else if (b < 0.20) {
			vipLevel = 1;
		}
		getPlayerService().deskPlayer.setVipLevel(vipLevel);
	}
	
	
	public void sendMessage(int messageHead, String messageContent) {
		
		switch (messageHead) {
		case SocketEnum.androidExitI:
			Receive_AndroidExit();
			break;
		default:
			break;
		}
		
		getPlayerService().sendMessage(messageHead, messageContent);
	}
	public void deal() {
		try {
//			switch (deskPlayer.getState()) {
//			case DictionaryTabel.player_waitNextJu:
//				checkAndorid();
//				break;
//			case DictionaryTabel.player_xiazhu:
//				operate_Bet();
//				break;
//			default:
//				break;
//			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
	private long playTime = 0;
	private boolean first = true;
	private void operate_Bet() {
		if (getPlayerService().deskPlayer.getBetInfo().size() == 0 && first){
			first = false;
			playTime = System.currentTimeMillis() + WDataConversion.randLong(androidManage.gameRoomService.configRoomService.androidBetTime[0],
					androidManage.gameRoomService.configRoomService.androidBetTime[1]);
		}
		
		if( playTime < System.currentTimeMillis()){
			int chipCount = androidManage.gameRoomService.configRoomService.chipType.length;
			chipCount = WDataConversion.randInt(0, chipCount);
			int sub = WDataConversion.randInt(0, 7);
			while (chipCount >= 0) {
				int betChip = androidManage.gameRoomService.configRoomService.chipType[chipCount];
//				if(operate_Bet(sub, betChip)){
//					break;
//				} else {
//					chipCount -- ;
//				}
			}
			
			if (getPlayerService().deskPlayer.getBetInfo().size() == 0)
				roomDeskService.leaveDesk(this);
			
			playTime = System.currentTimeMillis() 
					+ WDataConversion.randLong(androidManage.gameRoomService.configRoomService.androidBetTime[0],
							androidManage.gameRoomService.configRoomService.androidBetTime[1]);
			first = true;
		}
	}
	
	/**
	 * 机器人离开
	 */
	private void Receive_AndroidExit(){
		try {
			androidManage.androidServices_Useing.remove(this);	
			if (!androidManage.androidServices_Available.contains(this))
				androidManage.androidServices_Available.add(this);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	/**检测机器人是否卡死**/
	private void checkAndorid(){
		if (roomDeskService == null)
			Receive_AndroidExit();
		else {
			if (getPlayerService().deskPlayer.getDeskNum() == -1 || getPlayerService().deskPlayer.getSeatNum() == -1)
				Receive_AndroidExit();
			else {
				if (roomDeskService.deskPlayerServices[getPlayerService().deskPlayer.getSeatNum()] == null)
					Receive_AndroidExit();
				else {
					if (roomDeskService.deskPlayerServices[getPlayerService().deskPlayer.getSeatNum()].getPlayerService().deskPlayer.getPlayerId() != getPlayerService().deskPlayer.getPlayerId())
						Receive_AndroidExit();
				}
			}
		}
	}
	
	public static void main(String[] args) {
		int i = 0;
		while(i < 100) {
			System.out.println(WDataConversion.randInt(0, 7));
			i++;
		}
	}
}
